package com.itheima.service;
import com.itheima.entity.Comment;
import com.itheima.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Optional;


@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;
    @Cacheable("comment")


    public Comment findCommentById(Integer id){
    Optional<Comment> comment =commentRepository.findById(id);
    if (comment.isPresent()){
        return comment.get();
    }
    return null;
}
    @CachePut(value = "comment", key = "#id")
    public Comment updateComment(Integer id, String content){
        Comment comment = findCommentById(id);
        comment.setContent(content);
        commentRepository.updateComment(id, content);
        return comment;
    }
    @CacheEvict("comment")
    public void deleteCommentById(Integer id){
        commentRepository.deleteById(id);
    }
}

